<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\WatchBloodOxygen;
use app\common\model\WatchBloodPressure;
use app\common\model\WatchHeartRate;
use app\common\model\WatchTurnover;
use fast\Date;
use fast\Random;
use think\Config;
use think\Validate;

/**
 * 手表下发消息接口
 */
class Device extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            //$this->error(__('User center already closed'));
        }

    }

    public function advet()
    {
        $this->success('返回成功', model('app\admin\model\wanlshop\Advert')->select());

    }

    //手表上行数据接收
    public function receivedata(){
        $action = input('action');
        $deviceid = input('deviceid');
        $heart = input('heart');//心率
        $ox = input('ox');
        $systolic = input('systolic');//高压
        $diastolic = input('diastolic');//低压
        $turnover = input('turnover');//反转次数
        if($action=='sensor'){
            if(!empty($heart)){
                self::saveheart($deviceid,$heart);
            }
            if(!empty($ox)){
                self::saveox($deviceid,$ox);
            }
            if(!empty($systolic)&&!empty($diastolic)){
                self::savepressure($deviceid,$systolic,$diastolic);
            }
            if(!empty($turnover)){
                self::saveturnover($deviceid,$turnover);
            }

        }
        //lbs location
        if($action=='submitlocation'){
            model('\app\common\model\LocationlbsWatch')->save([
                'deviceid' => input('deviceid'),
                'longitude' => input('longitude'),
                'latitude' => input('latitude'),
                'address' => input('address'),
                'addresstype' => input('addresstype')
            ]);
        }
        //sportstep
        if($action=='submitsport'){
            model('\app\common\model\Sportstep')->save([
                'deviceid' => input('deviceid'),
                //'calorie' => input('sportdistance'),
                //'distance' => input('sportdistance'),
                'value' => input('sportstep')
            ]);
        }
        $this->success();
    }

    //存心率数据
    protected function saveheart($deviceid,$data){
        $heartRateModel = new WatchHeartRate();
        $savedata = [
            'value' =>$data,
            'deviceid' => $deviceid
        ];
        $heartRateModel->save($savedata);
    }

    //存血氧数据
    protected function saveox($deviceid,$data){
        $WatchBloodOxygenModel = new WatchBloodOxygen();
        $savedata = [
            'value' => $data,
            'deviceid' => $deviceid
        ];
        $WatchBloodOxygenModel->save($savedata);
    }

    //存血压数据
    protected function savepressure($deviceid,$systolic,$diastolic){
        $WatchBloodPressureModel = new WatchBloodPressure();
        $savedata = [
            'min_value' => $diastolic,
            'max_value' => $systolic,
            'deviceid' => $deviceid
        ];
        $WatchBloodPressureModel->save($savedata);
    }

    //存反转次数
    protected function saveturnover($deviceid,$data){
        $WatchTurnoverModel = new WatchTurnover();
        $savedata = [
            'value' => $data,
            'deviceid' => $deviceid
        ];
        $WatchTurnoverModel->save($savedata);
    }



}
